#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

Global $shop_ask[6] = [202,297, 697,30, 371,320] ; open x, open y, close x, close y, border x, border y
Global $shop_color[8] = [0x8F7249 , 0x060402, 0x060402, 0x8f7249, 0x41280F, 0x4B2800, 0xD8AC70,0x9E9E12] ; color border even,color cell even,color border odd,color cell odd, color empty cell, color empty caption
Global $shop_box[4] = [71,311,658,390]
Global $shop_item[4][2] = [[23,24],[41,22],[22,38],[38,36]]
Global $no_shop[5][5] = [ _
	[0x7D8928, 0x939E12, 0xA8A808, 0x9D9D11, 0x929D10], _
	[0x7D8928, 0x939E12, 0xA8A808, 0x93931C, 0x9D9D10], _
	[0x7D8928, 0x9E9E12, 0x9E9E12, 0x7C7C1C, 0x87921B], _
	[0x7D8928, 0x9E9E13, 0x9E9E12, 0x7C8827, 0x87921B], _
	[0x7E8929, 0x9E9E13, 0x939312, 0x7C881C, 0x929D1B] _
]

Func open_shop($pos)
	Local $p = $winlist[$pos]
	Local $color = PixelGetColor($p[0] + $p[2] + $shop_ask[0],$p[1] + $p[3] + $shop_ask[1])
	If $color = $shop_color[7] Then
		Return False
	Else
		make_click($pos,$shop_ask[0] + random_shift(2), $shop_ask[1] + random_shift(2))
		Return True
	EndIf
EndFunc

Func close_shop($pos)
   make_click($pos,$shop_ask[2] + random_shift(2),$shop_ask[3] + random_shift(2))
EndFunc

Func shop_opened($pos)
   Return slot_ready($pos)
EndFunc

Global $shop_tBits = False
Global $shop_data_width
Global $shop_data_length
Global $shop_type
Global $shop_count
Global $shop_item_array

Func get_shop_data($pos,$save = False)
	If $shop_tBits Then
		_WinAPI_DeleteObject($shop_tBits)
	EndIf
   
	Local $p = $winlist[$pos]
	Local _
		$l = $p[0] + $p[2] + $shop_box[0], _
		$t = $p[1] + $p[3] + $shop_box[1], _
		$r = $p[0] + $p[2] + $shop_box[2], _
		$b = $p[1] + $p[3] + $shop_box[3], _
		$w = $r - $l + 1, _
		$h = $b - $t + 1, _
		$length = $w * $h
	
	Local $hBMP = _ScreenCapture_Capture("",$l,$t,$r,$b)
	If $save Then
		_ScreenCapture_SaveImage($save,$hBMP, False)
		_DebugOut("store: " & $save)
	EndIf
	
	$shop_tBits = DllStructCreate('dword[' & $length & ']')
	If $shop_tBits = 0 Then
		_DebugOut("failed DllStructCreate e=" & @error & " l=" & $length)
	EndIf
	_WinAPI_GetBitmapBits($hBMP, 4 * $length, DllStructGetPtr($shop_tBits))
	$shop_data_width = $w
	$shop_data_length = $length
	_WinAPI_DeleteObject($hBMP)
EndFunc

Func check_shop_info($pos)
	Local $lx = $shop_ask[4] - $shop_box[0] - 29 * $shop_count
	Local $ly = $shop_ask[5] - $shop_box[1]
	
	_DebugOut("shop_info count: " & $shop_count)
	For $i = 0 To $shop_count - 1
		Local $c1 = getPixel($shop_tBits, $shop_data_width, $lx + 13, $ly + 29)
		Local $c2 = getPixel($shop_tBits, $shop_data_width, $lx + 13, $ly + 59)
		If $c1 = $shop_color[4] Then
			;_DebugOut($i & " empty cell")
			If $c2 = $shop_color[5] Then
				;_DebugOut($i & " and empty price")
			Else
				_DebugOut($i & " empty cell has price")
				Return False
			EndIf
		EndIf
		$lx += 58
	Next
	Return True
EndFunc

Func store_shop_info($pos)
	Local $ary[$shop_count]
	Local $lx = $shop_ask[4] - $shop_box[0] - 29 * $shop_count
	Local $ly = $shop_ask[5] - $shop_box[1]
	
	Local $mi = Floor($shop_count / 2)
	For $i = 0 To $shop_count - 1
		Local $c1 = getPixel($shop_tBits, $shop_data_width, $lx + 13, $ly + 29)
		Local $c2 = getPixel($shop_tBits, $shop_data_width, $lx + 13, $ly + 59)
		If $c1 = $shop_color[4] And $c2 = $shop_color[5] Then
			$ary[$i] = -1
		Else
			Local $ca[4], $zx = $lx, $zy = $ly - 9
			For $j = 0 To 3
				$ca[$j] = getPixel($shop_tBits, $shop_data_width, $zx + $shop_item[$j][0], $zy + $shop_item[$j][1])
			Next
			$ary[$i] = store_shop_item($pos,$zx,$zy,$ca,$i)
		EndIf
		$lx += 58
	Next
	$shop_item_array = $ary
	Return True
EndFunc

Func slot_ready($pos)
	Local $lx = $shop_ask[4] - $shop_box[0]
	Local $ly = $shop_ask[5] - $shop_box[1]
	Local $cp1 = getPixel($shop_tBits, $shop_data_width, $lx, $ly)
	Local $cp2 = getPixel($shop_tBits, $shop_data_width, $lx + 29, $ly)
	
	If $test > 1 Then
		_DebugOut("slot: " & $lx & "," & $ly & " c1 = 0x" & Hex($cp1,6) & " c2 = 0x" & Hex($cp2,6))
		_DebugOut("need: " & $lx & "," & $ly & " c1 = 0x" & Hex($shop_color[0],6) & " c2 = 0x" & Hex($shop_color[1],6))
	EndIf
	
	$shop_type = 0
	$shop_count = 0
	If $cp1 = $shop_color[0] And $cp2 = $shop_color[1] Then
		$lx += 29
		$shop_count = 2
		$shop_type = 1
	ElseIf $cp1 = $shop_color[2] And ($cp2 = $shop_color[3] Or $cp2 = $shop_color[6]) Then
		$shop_count = 1
		$shop_type = 2
	EndIf
	If $shop_type > 0 Then
		While $lx < $shop_box[2] - $shop_box[0] + 1
			$lx += 58
			Local $tmp = getPixel($shop_tBits, $shop_data_width, $lx, $ly)
			If  $tmp = $shop_color[1] Then
				Local $c1 = getPixel($shop_tBits, $shop_data_width, $lx -16, $ly + 29)
				Local $c2 = getPixel($shop_tBits, $shop_data_width, $lx -16, $ly + 59)
				Local $dx = $lx - 16
				$shop_count += 2
				If $test > 1 Then
					_DebugOut("compare 0x" & Hex($tmp,6) & " = 0x" & Hex($shop_color[1],6) & " c1 = 0x" & Hex($c1,6) & " c2 = 0x" & Hex($c2,6))
				EndIf
			Else
				ExitLoop
			EndIf
		WEnd
		;_DebugOut("slot(" & $shop_type & "): " & $shop_count)
		return True
	Else
	   Return False
	EndIf
EndFunc

Func shop_empty_place($pos,$place)
	If $place < 0 _
	Or $place >= $shop_count _
	Or $shop_item_array[$place] < 0 Then
		Return True
	Else
		Return False
	EndIf
EndFunc

Func shop_has_item($pos, $place, ByRef $ary)
	_DebugOut("check for item " & $shop_item_array[$place] & " on place " & $place & " for " & $ary[0][0])
	For $i = 1 To $ary[0][0]
		_DebugOut("need " & $ary[$i][0] & "-" & $ary[$i][1])
		If $ary[$i][0] = $shop_item_array[$place] Then
			If $ary[$i][1] <= 0 Then
			   _DebugOut("found " & $ary[$i][0] & " without level")
				Return $shop_item_array[$place] & "-0"
			Else
				Local $l = get_item_level($pos,$place)
				_DebugOut("found " & $ary[$i][0] & " with level " & $l & " but need " & $ary[$i][1])
				If $l <= $ary[$i][1] Then
					Return $shop_item_array[$place] & "-" & $l
				EndIf
			EndIf
		EndIf
	Next
	Return False
EndFunc
